package morning.cat.study.quartz;

import morning.cat.study.quartz.job.MyJobTask;
import morning.cat.study.quartz.util.DateUtil;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Hello world!
 *
 */
public class Test5CronTrigger {
	public static void main(String[] args) {

		DateUtil.printNowForString("Scheduler start");

		// 1.JobDetail
		JobDetail myJobDetail = JobBuilder.newJob(MyJobTask.class)
				.withIdentity("myJob", "JobDetailGroup").build();

		// 2.Trigger
		// cron表达式生成器  http://www.pdtools.net/tools/becron.jsp
		Trigger myTrigger = TriggerBuilder.newTrigger()
				.withIdentity("myTrigger", "TriggerGroup")
				.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *")).build();

		// 3.Scheduler
		SchedulerFactory factory = new StdSchedulerFactory();

		try {
			Scheduler scheduler = factory.getScheduler();
			scheduler.start();
			scheduler.scheduleJob(myJobDetail, myTrigger);
			
			//挂起  可以被重启
			Thread.sleep(5000);
			scheduler.standby();
			
			//重启
			Thread.sleep(5000);
			scheduler.start();
			
			//关闭  不可以被重启
			Thread.sleep(5000);
			scheduler.shutdown(true);//优雅的关闭
			//scheduler.shutdown(false);//强制关闭 
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
